package com.fm.jbsql.util;

import java.util.logging.Logger;
import java.util.ResourceBundle;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: syaroshenko
 * Date: Jul 13, 2006
 * Time: 11:36:27 AM
 * Project specific exeption. Used to visualize thrown exeption in form of "ERROR number + message string"
 */

/**
 * Project specific exeption. Used to visualize thrown exeption in form of "ERROR number + message string"
 */
public final class JbsqlException extends Exception {
    private static final Logger logger = Logger.getLogger("com.fm.jbsql.util.JbsqlException");
    /**
     * Constructor.
     * @param num exception number
     * @param msg format string
     * @param ex arguments string according to format
     */
    public JbsqlException(String num, String msg,  Throwable ex) {
        super(ex);
        if ( num != null ) {
            String str;
            try {
                ResourceBundle labels = ResourceBundle.getBundle("com.fm.jbsql.resources.LabelsBundle");
                str = labels.getString(num);
            }
            catch (Exception e) {
                System.out.println("ERROR: failed to load localized string; " + e.toString());
                str = num;
            }
            if ( msg != null ) str = str + " "+ msg;
            System.out.println("Jbsql reports error[" + num + "]: " + str);   logger.fine("ERROR[" + num + "]: " + str);
        }

        if (ex instanceof SQLException) {
            SQLException sqe = (SQLException) ex;
            System.out.println("SQLException: " + sqe.getMessage());    logger.fine("SQLException: " + sqe.getMessage());
            System.out.println("SQLState: " + sqe.getSQLState());       logger.fine("SQLState: " + sqe.getSQLState());
            System.out.println("VendorError: " + sqe.getErrorCode());   logger.fine("VendorError: " + sqe.getErrorCode());
        }
        if ( ex != null ) {
            System.out.println(ex.toString());
            logger.severe("\t" + ex.toString());
        }
    }
}
